package com.sky.controller.user;

import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author bairimengchang
 * @version 1.0
 * @project sky-take-out
 * @description C端订单操作控制器
 * @date 2024/08/26 15:25:13
 */
@RestController
@RequestMapping("/user/order")
@Api(tags = "C端订单接口")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 用户下单
     * @param ordersSubmitDTO 订单提交传输数据模型
     * @return 用户订单提交返回的数据对象
     */
    @PostMapping("/submit")
    @ApiOperation("用户下单")
    public Result<OrderSubmitVO> submitOrder(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        // TODO前端参数中对于deliveryStatus配送状态  1立即送出  0选择具体时间。全部都是0，需要修改
        log.info("用户下单：{}",ordersSubmitDTO);
        OrderSubmitVO orderSubmitVO = orderService.submitOrder(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }


    /**
     * 订单支付
     * @param ordersPaymentDTO 订单支付传输数据模型
     * @return 预支付交易单视图对象
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> orderPayment(@RequestBody OrdersPaymentDTO ordersPaymentDTO){
        log.info("订单支付：{}",ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.orderPayment(ordersPaymentDTO);
        log.info("预支付交易单：{}",orderPaymentVO);
        //这里目前返回空对象处理
        return Result.success(orderPaymentVO);
    }

    /**
     * 查询订单详情
     * @param id 订单id
     * @return 订单详情
     */
    @GetMapping("/orderDetail/{id}")
    @ApiOperation("订单详情视图对象")
    public Result<OrderVO> orderDetail(@PathVariable("id") Long id){
        log.info("查询订单详情：{}",id);
        OrderVO orderVO = orderService.orderDetail(id);
        return Result.success(orderVO);
    }

    /**
     * 取消订单
     * @param id 订单id
     * @return 订单取消结果
     */
    @PutMapping("/cancel/{id}")
    @ApiOperation("取消订单")
    public Result<Object> cancelOrder(@PathVariable("id") Long id){
        log.info("取消订单：{}",id);
        boolean b = orderService.cancelOrder(id);
        if (b) {
            return Result.success("取消订单成功！");
        }
        return Result.error("取消订单失败，后端控制器报错！");
    }

    /**
     * 历史订单查询
     * @param ordersPageQueryDTO 查询条件传输数据模型
     * @return 历史订单分页查询返回的数据对象
     */
    @GetMapping("/historyOrders")
    @ApiOperation("历史订单查询")
    public Result<PageResult> getOrdersPage(OrdersPageQueryDTO ordersPageQueryDTO){
        // 这里只传递了 page和pageSize，status其他条件为空
        log.info("查询历史订单：{}",ordersPageQueryDTO);
        PageResult pageResult = orderService.getOrdersPage(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 再来一单
     * @param id 订单id
     * @return 再来一单结果
     */
    @PostMapping("/repetition/{id}")
    @ApiOperation("再来一单")
    public Result<String> repetition(@PathVariable("id") Long id){
        log.info("再来一单：{}",id);
        boolean b = orderService.repetition(id);
        if (b) {
            return Result.success("再来一单成功！");
        }
        return Result.error("再来一单失败，后端控制器报错！");
    }

    /**
     * 用户催单
     * @param id 订单id
     * @return 催单成功无提示信息
     */
    @GetMapping("/reminder/{id}")
    @ApiOperation("用户催单")
    public Result<String> reminder(@PathVariable("id") Long id){
        log.info("用户催单：{}",id);
        orderService.reminder(id);
        return Result.success();
    }
}
